<?php
/*
 * cui:phpstorm
 * 2021/7/29
 * 15:38
 * User: s1mple_qcloud@163.com
 *
 */

namespace App\Api\Controllers;


use App\Api\Models\LiveAnchor;
use App\Api\Models\Member;
use App\Api\Services\BlackWhite;
use App\Api\Services\JsonService;
use App\Api\Services\Live;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class LiveController
{
    /**
     *  cuiqun
     *  显示视频列表
     */
    public function getLiveList()
    {
        
        $data = Live::serGetList(config('live.order'),config('live.order_line'));
        if (empty($data)){
            return JsonService::fail('查询失败',[],config('code.HTTP_FAIL'));
        }
        return JsonService::success('查询成功',$data,config('code.HTTP_OK'));
    }

    /**
     * cuiqun
     * 根据id获取单个主播信息
     * @param Request $request
     * @return array
     */
    public function getAnchor(Request $request)
    {
        $validat = $request->validate([
            'id' => ['required'],
        ]);
        //验证是否合法
        $validation = new BlackWhite();
        $legal = $validation->isSafeIp();
        if(!$legal){
            return JsonService::fail('限制访问');
        }
        $id = (int)$validat['id'];
        // 查询id 是否有主播开播
        $getAnchor=Live::getStudio($id);
        if (!$getAnchor) return JsonService::fail('没有这个主播,可以去看看其他主播');
        return JsonService::success('查询成功',$getAnchor);
    }

    /**
     * cuiqun
     * 将弹幕存到缓存
     * @param Request $request
     * @return array
     */
    public function barrage(Request $request)
    {
        $barrage = $request->get('barrage');
        $id = $request->get('id');
        // 将弹幕存到redis中
        Redis::lpush($id,$barrage);
        // 取出该主播的弹幕
        $data = Redis::lpop($id);
        if (empty($data)) return JsonService::fail('无法获取当前直播弹幕','',config('code.HTTP_FAIL'));
        return JsonService::success('查询成功',$data,config('code.HTTP_OK'));
    }

    /**
     * cuiqun
     * 直播回放地址
     */
    public function playBackAddress(Request $request)
    {
        $id = $request->get('uid');
        if (empty($id) || !is_numeric($id)){
            return JsonService::fail('数据格式不正确','',config('code.HTTP_FAIL'));
        }
        $data = Live::getPlayBack($id);
        if ($data){
            return JsonService::success('查询成功',$data,config('code.HTTP_OK'));
        }else{
            return JsonService::fail('查询失败，该主播没有回放','',config('code.HTTP_FAIL'));
        }
    }

    /**
     * cuiqun
     * 查询单条回放地址
     * @param Request $request
     * @return array
     */
    public function playBackOnce(Request $request)
    {
        $id =$request->get('id');
        if (empty($id) || !is_numeric($id)){
            return JsonService::fail('数据格式不对','',config('code.HTTP_FAIL'));
        }
        $data = Live::playBackOnce($id);
        if ($data){
            return JsonService::success('查询成功',$data,config('code.HTTP_OK'));
        }else{
            return JsonService::fail('当前主播没有回放，可以看看其他主播','',config('code.HTTP_FAIL'));
        }
    }
    /**
     * cuiqun
     * 关闭直播间
     * @param Request $request
     * @return array
     */
    public function LiveOver(Request $request)
    {
        // 获取用户id
        $id = $request->get('member_id');
        // 获取主播id
        $liveId = $request->get('id');
        // 判断是不是用户
        if (! Member::findMember($id)){
            return JsonService::fail('您不是用户，请先注册','',1002);
        }else{
            // 直播当前状态
            $isPlay = LiveAnchor::IsplayStatus($liveId);
            if ($isPlay <= 0){
                return JsonService::fail('当前直播间正在直播','',config('code.HTTP_ALREADY_EXISTED'));
            }else{
                $updateStatus = LiveAnchor::LiveOver($liveId);
                return JsonService::success('直播间关闭成功','',config('code.HTTP_OK'));
            }
        }
    }
}